package think.net2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class Server {
	public static void main(String[] args) throws IOException {
		@SuppressWarnings("resource")
		final ServerSocket server = new ServerSocket(8787);
		List<Socket> list = new ArrayList<>();
		while(true){
			Socket soc = server.accept();
			list.add(soc);
			new Thread(()->{
				System.out.println("---------------------");
				System.out.println(soc.getInetAddress()+":"+soc.getPort());
				try (
						BufferedReader reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
						PrintWriter pw = new PrintWriter(soc.getOutputStream());) {
					String in = null;
					do {
						in = reader.readLine();
						System.out.println("input: " + in);
						pw.println(in.toUpperCase());
						pw.flush();
					}while(StringUtils.isNotBlank(in));
					
				}catch(Exception e){
					e.printStackTrace();
				}
			}).start();
		}
	}
}
